#!/usr/bin/perl -w

#---------------------------------------------------------------------- 
#
# this script replaces a given string with another in the list of
# files passed to it. It is based on work done by Mark Pulver at
# Austek Microsystems in 1991.
# 
# $Copyright: (c) 1997-2008 Thomas Dejanovic $ 
#
#---------------------------------------------------------------------- 

# get the string to be replaced

if (@ARGV) {
    $target = shift @ARGV;
} else {
    print "usage : replaceAll <target> <new text> <file name> [<file name>]\n";
}

if (@ARGV) {
    $new = shift @ARGV;
} else {
    print "usage : replaceAll <target> <new text> <file name> [<file name>]\n";
}

if (@ARGV) {
    while (@ARGV) {
	$file_name = shift @ARGV;

	# open the file to read.
	if (!open(OLD, "<$file_name")) {
	    print STDERR "ERROR : could not open file $file_name\n";
	    exit 1;
	};

	# open the file to write
	if (!open(NEW, ">$file_name.tmp")) {
	    print STDERR "ERROR : could not create file $file_name\n";
	    close(OLD);
	    exit 1;
	};

	print "*    Processing file $file_name\n";
	while (<OLD>) {
	    s/$target/$new/g;
	    print NEW;
	}

	close(OLD);
	close(NEW);

	# rename the old file to the new one.
	if (!rename("$file_name.tmp", $file_name)) {
	    print STDERR "ERROR : could not rename $file_name.tmp to $file_name\n";
	    exit 1;
	}
    }
} else {
    print "usage : replaceAll <target> <new text> <file name> [<file name>]\n";
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Local Variables:
# mode: perl
# End:



